package com.ala4.oxcafe.common.utils.event;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author PING
 * @date 2025/8/17 13:50
 */
@Component
public class SpringEventUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    /**
     * 发布Spring事件（支持任意类型事件对象）
     *
     * @param event 事件对象（可以是任意类型，包括ApplicationEvent子类或普通POJO）
     */
    public static void publishEvent(Object event) {
        if (applicationContext != null) {
            applicationContext.publishEvent(event);
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringEventUtil.applicationContext = applicationContext;
    }

}
